package com.fxp.project.jiuselu.web.business.v1.controller.message;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.fxp.project.jiuselu.web.base.controller.BaseController;
import com.fxp.project.jiuselu.web.domain.CommonResult;
import com.fxp.project.jiuselu.web.models.message.entity.JslMessageUse;
import com.fxp.project.jiuselu.web.models.message.service.IJslMessageUseService;
import com.fxp.project.jiuselu.web.models.user.entity.JslUser;
import com.fxp.project.jiuselu.web.models.user.service.IJslUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/v1/messageuse")
public class JslMessageUseController extends BaseController {

    @Autowired
    private IJslMessageUseService jslMessageUseService;
    @Autowired
    private IJslUserService jslUserService;

    @PostMapping("/list")
    @CrossOrigin
    public CommonResult list(@Validated JslMessageUse jslMessageUse,BindingResult result){
        super.checkBindResult(result);
        List<JslMessageUse> jslMessageUses = jslMessageUseService.selectMessageUseByUserId(super.getCurrentUserInfo().getUserId());
        return CommonResult.success(jslMessageUses);
    }

    @PostMapping("/save")
    @CrossOrigin
    public CommonResult save(@RequestBody @Validated JslMessageUse jslMessageUse,BindingResult result){
        //mId2为0时,表明是注册是走的接口,不能通过token拿到userId,可以通过注册时填的手机号
        if(jslMessageUse.getMId2()==0){
            JslUser jslUser = jslUserService.selectByUserPhoneAndLevle(jslMessageUse.getPhone(),0);
            Long userId = jslUser.getUserId();
            jslMessageUse.setUserId(userId);
        }else {
            super.checkBindResult(result);
            jslMessageUse.setUserId(super.getCurrentUserInfo().getUserId());
        }
        jslMessageUse.setCreateTime(LocalDateTime.now());
        int i = jslMessageUseService.insertJslMessageUse(jslMessageUse);
        if(i>0){
            return CommonResult.success("操作成功");
        }else {
            return CommonResult.failed("操作失败,请返回重试!");
        }
    }

    @PutMapping("/edit")
    @CrossOrigin
    public CommonResult update(@RequestBody @Validated JslMessageUse jslMessageUse,BindingResult result){
        super.checkBindResult(result);
        jslMessageUse.setUserId(super.getCurrentUserInfo().getUserId());
        jslMessageUse.setUpdateTime(LocalDateTime.now());
        int i = jslMessageUseService.updateJslMessageUse(jslMessageUse);
        if(i>0){
            return CommonResult.success("操作成功");
        }else {
            return CommonResult.failed("操作失败,请返回重试");
        }
    }

    @PostMapping("/edit/isRead")
    @CrossOrigin
    public CommonResult isRead(@Validated JslMessageUse jslMessageUse,BindingResult result){
        super.checkBindResult(result);
        int i = jslMessageUseService.updateJslMessageUseIsRead(super.getCurrentUserInfo().getUserId());
            return CommonResult.success("操作成功");
    }
}
